/******************************************************************************
 *  _____     _____
 *  \  __\    \  __\
 *   \ \_\\    \ \_\\
 *    \  __\SS  \  __\roject
 *     \_\       \_\
 *
 * Mateusz Maciąg, Rafał Urbański
 * Komputerowe Systemy Sterowania
 ******************************************************************************/

#include <QtGui/QApplication>
#include "UI/MainWindow.h"
#include "UI/SimulatorSettings.h"

#include "SimEngine/SimulatorEngine.h"

#include <iostream>
#include <fstream>
#include <memory>
#include <map>
#include "Utils.h"

#include "SimFile/ConfigWriter.h"
#include "SimFile/ConfigReader.h"

#include <sstream>

#include "plant.h"

using namespace std;
using namespace RafMat;
using namespace RafMat::SimInterface;

int testApp(int argc, char *argv[]);

int runApp(int argc, char *argv[])
{
    auto_ptr<QApplication> app(new QApplication(argc, argv));
    auto_ptr<MainWindow> window(new MainWindow());
    window->show();

    // dodać blok try catch
    // bo qt nie specjalnie jest bezpieczny
    // jeśli chodzi o wyjątki
    // Mozna dodac np. awaryjne zapisanie projektu

    return app->exec();
}

int main(int argc, char *argv[])
{
     //return testApp(argc, argv);

    cout << "IConfirable instances before start: "
         << IConfigurable::debugGetLivingCount() << endl;

    int result = runApp(argc, argv);

    cout << "IConfirable instances after app exit: "
         << IConfigurable::debugGetLivingCount() << endl;

    return result;
}

using namespace RafMat::SimFile;

const string testStr = "Formatting ===    information\n"
        "format flags: a set of internal indicators describing how certain input/output operations shall be interpreted or generated. The state of these indicators can be obtained or modified by calling the members flags, setf and unsetf, or by using manipulators.\n"
        "field width: describes the width of the next element to be output. This value can be obtained/modified by calling the member function width or parameterized manipulator setw.\n"
        "display precision: describes the decimal precision to be used to output floating-point values. This value can be obtained/modified by calling member precision or parameterized manipulator setprecision.\n"
        "fill character: character used to pad a field up to the field width. It can be obtained or modified by calling member function fill or parameterized manipulator setfill.\n"
        "locale object: describes the localization properties to be considered when formatting i/o operations. The locale object used can be obtained calling member getloc and modified using member imbue.\n";

int testApp(int argc, char *argv[])
{
    (void) argc;
    (void) argv;

    AdaptiveTechniques::Plant magicPlant;
    magicPlant.setParameter("samplePeriod", "0.01");
    magicPlant.setParameter("disturbances", "0.2"); // 0 - 1

    std::fstream file;

    file.open("data.csv", std::ios::out|std::ios::app);

    double y, u;
    double u_w[10] = {1, 1, 1, 0.2, 0.2, 1, 1, 0.6, 0.3, 0};
    for(int j = 0; j < 10; j++) {
        u = u_w[j];
        for(int i = 0; i <2000; i++)
        {
            y = magicPlant.simulate(u);
            file << u << ";" << y << "\n";
        }
    }

    file.close();

    return 0;
}
